from django import template

register = template.Library()

@register.filter_function
def order_by(queryset, args):
    args = [x.strip() for x in args.split(',')]
    return queryset.order_by(*args)

@register.filter_function
def reverse(queryset):
    return queryset.reverse()


@register.filter_function
def call(obj, methodName):
    method = getattr(obj, methodName)
    args = getattr(obj, "__callArg", None)
    if args:
        ret = method(*args)
        del obj.__callArg
        return ret
    return method()

@register.filter_function
def args(obj, arg):
    if not obj.__dict__.has_key("__callArg"):
        obj.__callArg = []
    obj.__callArg += [arg]
    return obj
